package com.qifan.alb2b.action.back;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qifan.alb2b.domain.AdminInfoDomain;
import com.qifan.alb2b.domain.NewsInfoDomain;
import com.qifan.alb2b.service.NewsInfoService;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.ID;
import com.qifan.alb2b.util.Page;

@Controller
@RequestMapping("/admin/news")
public class NewsManageController{
	private NewsInfoService newsInfoService;

	public void setNewsInfoService(NewsInfoService newsInfoService) {
		this.newsInfoService = newsInfoService;
	}
	
	@RequestMapping("/query.adm")
	@ResponseBody
	public Page query(DataGridModel dgm,NewsInfoDomain uqp) {
		//spring太给力了，可以自动装配两个对象  会自动的装返回的Map转换成JSON对象
		return newsInfoService.query(dgm,uqp);
	}
	
	@RequestMapping("/add.adm")
	@ResponseBody
	public Map<String, Object> add(HttpServletRequest request,NewsInfoDomain newsInfoDomain){
		AdminInfoDomain admin =(AdminInfoDomain)request.getSession().getAttribute(Constants.SESSION_ADMIN);
		Map<String, Object> map=new HashMap<String, Object>();
		newsInfoDomain.setNewsId(ID.next()+"");//设置唯一ID
		
		newsInfoDomain.setStatus(Constants.NOMARL);
		newsInfoDomain.setPublicTime(new Date());//设置创建日期
		newsInfoDomain.setPublicAdmin(admin.getAdminName());
		newsInfoService.add(newsInfoDomain);
		map.put("success", true);
		return map;
	}
	
	@RequestMapping("/update.adm")
	@ResponseBody
	public Map<String, Object> update(NewsInfoDomain newsInfoDomain){
		Map<String, Object> map=new HashMap<String, Object>();
		NewsInfoDomain newsInfo= newsInfoService.getNewsById(newsInfoDomain.getNewsId()).get(0);
		newsInfoDomain.setStatus(newsInfo.getStatus());
		newsInfoDomain.setPublicTime(new Date());//设置创建日期
		newsInfoService.update(newsInfoDomain);
		map.put("success", true);	
		return map;
	}
	
	@RequestMapping("/del.adm")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam("uid") String[] ids){
		Map<String, Object> map=new HashMap<String, Object>();
		newsInfoService.delete(ids);
		map.put("success", true);
		return map;
	}
	
	@RequestMapping("/updateStatus.adm")
	@ResponseBody
	public Map<String, Object> updateStatus(@RequestParam("uid") String[] uid,@RequestParam("status") String status){
		Map<String, Object> map=new HashMap<String, Object>();
		newsInfoService.updateStatus(uid,status);
		map.put("success", true);
		return map;
	}
	
}
